﻿#region imports

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CondeNet.AssetManager.Data;
using CondeNet.AssetManager.Business;
using CondeNet.AssetManager.Business.Interface;
using CondeNet.AssetManager.Business.Model;
using CondeNet.AssetManager.DataProvider.Sql;
using CondeNet.AssetManager.DataProvider.Sql.Mappers;

#endregion

namespace CondeNet.AssetManager.DataProvider.Sql.Models
{
    public class AssetMetaDataModelProvider : MetaDataModelProvider
    {
        #region constructors

        public AssetMetaDataModelProvider ( MSSqlProvider provider ) : base ( provider ) { }

        #endregion

        #region methods

        /// <summary>
        /// Get single AssetMetaDataModel object by passing the Id.
        /// </summary>
        /// <param name="assetMetaDataId"></param>
        /// <returns>AssetMetaDataModel object with nested MetaData object.</returns>
        public AssetMetaDataModel GetAssetMetaDataByID ( int assetMetaDataId )
        {
            return BuildAssetMetaDataModel ( provider.GetAssetMetaDataByID ( assetMetaDataId ) );
        }

        /// <summary>
        /// Get All the AssetMetaDataModel objects
        /// </summary>
        /// <returns>List of AssetMetaDataModel objects, each with a nested MetaData object.</returns>
        public List<AssetMetaDataModel> GetAllAssetMetaData ( )
        {
            return BuildAssetMetaDataModelList ( provider.GetAllAssetMetaData ( ) );
        }

        /// <summary>
        /// Get All the AssetMetaDataModel objects with passed assetid
        /// </summary>
        /// <param name="assetId"></param>
        /// <returns>List of AssetMetaDataModel objects with passed assetId, each with a nested MetaData object.</returns>
        public List<AssetMetaDataModel> GetAssetMetaDataByAssetID ( int assetId )
        {
            return BuildAssetMetaDataModelList ( provider.GetAssetMetaDataByAssetID ( assetId ) );
        }

        /// <summary>
        /// Get All the AssetMetaDataModel objects with passed assetid and statusid
        /// </summary>
        /// <param name="assetId"></param>
        /// <param name="statusId"></param>
        /// <returns>List of AssetMetaDataModel objects with passed assetId and statusid, each with a nested MetaData object.</returns>
        public List<AssetMetaDataModel> GetAssetMetaDataByAssetIDStatusID ( int assetId , int statusId )
        {
            return BuildAssetMetaDataModelList ( provider.GetAssetMetaDataByAssetIDStatusID ( assetId , statusId ) );
        }

        /// <summary>
        /// Get All the AssetMetaDataModel objects with passed assetid and languageid
        /// </summary>
        /// <param name="assetId"></param>
        /// <param name="statusId"></param>
        /// <returns>List of AssetMetaDataModel objects with passed assetId and languageid, each with a nested MetaData object.</returns>
        public List<AssetMetaDataModel> GetAssetMetaDataByAssetIDLanguageID ( int assetId , int languageId )
        {
            return BuildAssetMetaDataModelList ( provider.GetAssetMetaDataByAssetIDLanguageID ( assetId , languageId ) );
        }

        /// <summary>
        /// Generates a list of AssetMetaDataModel objects by passing each item in the 
        /// list of AssetMetaDataobjects to the BuildAssetMetaDataModel method.
        /// </summary>
        /// <param name="assetMetaDataList"></param>
        /// <returns>List of AssetMetaDataModel objects, each with a nested MetaData object.</returns>
        public List<AssetMetaDataModel> BuildAssetMetaDataModelList ( IList<IAssetMetaData> assetMetaDataList )
        {
            List<AssetMetaDataModel> assetMetaDataModelList = new List<AssetMetaDataModel> ( );

            foreach ( AssetMetaData amd in assetMetaDataList )
            {
                assetMetaDataModelList.Add ( BuildAssetMetaDataModel ( amd ) );
            }

            return assetMetaDataModelList;
        }

        /// <summary>
        /// Builds a AssetMetaDataModel obejct from passed AssetMetaData object by sending that
        /// object to the base constructor and retrieving the MetaKey object to append to the AssetMetaDataModel 
        /// object depending on the value of the MetaDataID propery.
        /// </summary>
        /// <param name="assetMetaData"></param>
        /// <returns>An AssetMetaDataModel object with nested objects  depending on values</returns>
        private AssetMetaDataModel BuildAssetMetaDataModel ( IAssetMetaData assetMetaData )
        {
            MetaDataModel metaDataModel = base.BuildMetaDataModel ( provider.GetMetaDataByID ( assetMetaData.MetaDataID ) );

            if ( metaDataModel != null ) return new AssetMetaDataModel ( assetMetaData , metaDataModel );
            else return new AssetMetaDataModel ( assetMetaData );
        }

        #endregion
    }
}